<template>
  <section class="app-inner">
    <el-card class="header-card">
      <el-row type="flex" class="row-bg" justify="space-between">
        <el-col :span="18"><h2 style="margin-bottom:20px">查看巡检任务</h2></el-col>
        <el-col :span="3" style="text-align: right;"
          ><el-button type="success" @click="resetForm('ruleForm')" size="small" class="return-btn"
            >返回</el-button
          ></el-col
        >
      </el-row>
      <el-form :model="ruleForm" ref="ruleForm" label-width="500px" class="demo-ruleForm">
        <el-form-item label="任务编号">
          {{ ruleForm.taskCode }}
        </el-form-item>
        <el-form-item label="任务类型">
          {{ getName(ruleForm.taskType) }}
        </el-form-item>
        <el-form-item label="任务名称">
          {{ ruleForm.taskName }}
        </el-form-item>
        <el-form-item label="巡检执行人">
          {{ ruleForm.taskOperatorUserName }}
        </el-form-item>
        <el-form-item label="计划开启巡检时间">
          {{ ruleForm.startTime }}
        </el-form-item>
        <el-form-item label="实际完成时间">
          <template v-if="taskCompleteState == 2">{{ ruleForm.completeTime }}</template>
        </el-form-item>
        <el-form-item label="处理状态">
          <template v-if="ruleForm.isOvertime == 1">超时</template>
          <div v-else>{{ getLable(taskCompleteState) }}</div>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="table-card">
      <h2>巡检情况反馈</h2>
      <div v-for="(item, index) in spotArray" :key="index" class="position-tab">
        <div>
          <h3>{{ index + 1 }}</h3>
          <span>{{ item.spotName }}</span>
          <template v-if="item.hasException == 1">
            <el-button type="danger" size="mini">巡检异常</el-button>
            情况反馈：<span>{{ item.exceptionInfo }}</span>
          </template>
        </div>
        <el-table
          v-if="ruleForm.taskType == 2"
          border
          :data="item.taskSpotEventRelationList"
          :key="index"
          style="width: 100%"
        >
          <el-table-column type="index" label="序号" width="50"></el-table-column>
          <el-table-column prop="taskContent" label="巡查内容"></el-table-column>
          <el-table-column prop="isComplete" label="反馈结果">
            <template slot-scope="scope">
              <span v-if="ruleForm.taskCompleteState == 2">
                <span v-if="scope.row.inputMode">
                  <span v-if="scope.row.feedbackItemId === 1">
                    <span v-if="scope.row.isComplete">是</span>
                    <span v-else>否</span>
                  </span>
                  <span v-if="scope.row.feedbackItemId === 2">
                    <span v-if="scope.row.isComplete">确认</span>
                    <span v-else>取消</span>
                  </span>
                  <span v-if="scope.row.feedbackItemId === 3">
                    <span v-if="scope.row.isComplete">正常</span>
                    <span v-else>异常</span>
                  </span>
                </span>
                <span v-else>{{ scope.row.feedbackInputContent }}</span>
              </span>
            </template>
          </el-table-column>
          <el-table-column prop="feedbackImage" label="反馈照片">
            <template slot-scope="scope" v-if="ruleForm.taskCompleteState == 2">
              <viewer :images="scope.row.feedbackImage">
                <img v-for="src in scope.row.feedbackImage" :key="src" :src="src" class="img-over" />
              </viewer>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
  </section>
</template>

<script>
import { getTaskInfo } from './js/patrol'
import { completeState, planTypeList } from './js/constants'
export default {
  name: 'InspectDetail',
  data () {
    return {
      spotArray: [],
      openFlag: true,
      ruleForm: {
        taskCode: '',
        taskName: '',
        userName: '',
        spotCode: '',
        region: '',
        date1: '',
        date2: '',
        startTime: '',
        endTime: '',
        delivery: false,
        type: [],
        resource: '',
        desc: '',
        tag: '',
        taskCompleteState: '',
        taskType: ''
      }
    }
  },
  created () {
    this.ruleForm = this.$route.query
    this.taskCompleteState = this.ruleForm.taskCompleteState
    switch (this.ruleForm.taskCompleteState) {
      case 0:
        this.ruleForm.taskCompleteState = '待巡检'
        break
      case 1:
        this.ruleForm.taskCompleteState = '未完成'
        break
      case 2:
        this.ruleForm.taskCompleteState = '已完成'
        break
    }
    if (this.ruleForm.taskCode) {
      window.localStorage.setItem('patrolDetail', JSON.stringify(this.ruleForm))
    } else {
      this.ruleForm = JSON.parse(window.localStorage.getItem('patrolDetail'))
    }
    this.getSpotList()
  },
  methods: {
    formatter (row, column, cellValue) {
      return this.$base.constFormatter(completeState, cellValue)
    },
    async getSpotList () {
      let res = await getTaskInfo(this.ruleForm.taskCode)
      res = res.data.taskSpotEventList
      for (let j in res) {
        res[j].taskSpotEventRelationList.forEach(item => {
          // feedbackImage: "https://dataharbour.unismartcity.com:8000/standard-platform/c121b470d8e045cab7c9e689c38c5257.jpg"
          // item.feedbackImage = 'https://dataharbour.unismartcity.com:8000/standard-platform/c121b470d8e045cab7c9e689c38c5257.jpg'
          if (item.feedbackImage) {
            item.feedbackImage = item.feedbackImage.split(';')
            for (let i = 0; i < item.feedbackImage.length; i++) {
              if (item.feedbackImage[i] === '') {
                item.feedbackImage.splice(i, 1)
              }
            }
          }
        })
      }
      this.spotArray = res
      if (res.code === 1) {
        this.$message.error(res.msg)
        this.resetForm()
      }
    },
    resetForm () {
      if (this.ruleForm.routerFlag === 0) {
        // this.$router.push({ name: '/Patrol/InspectList' })
        this.$router.push({ path: 'InspectList' })
      } else {
        this.$router.push({ path: 'TempTask' })
      }
    },

    // 获取name
    getName (val) {
      return this.$base.constFormatter(planTypeList, +val)
    },

    getLable (val) {
      // 2代表已完成，1代表未完成，0表示待巡检
      let arr = ['待巡检', '未完成', '已完成']
      return arr[+val]
    }
  }
}
</script>

<style lang="scss" scoped>
.app-inner {
  height: 100%;
  font-size: 16px;
  .header-card,
  .table-card,
  .form-card,
  .foot-card {
    margin-bottom: 10px;
    .el-form {
      .el-form-item {
        .el-input {
          width: 160px;
        }
      }
      // .createBtn {
      //   float: right;
      // }
    }
  }
  .table-card {
    .position-tab {
      div {
        margin: 10px 0;
        h3 {
          display: inline-block;
          margin-right: 10px;
        }
        .el-button {
          margin: 0 10px;
          border-radius: 15px;
        }
        .img-over {
          margin-right: 5px;
          width: 60px;
          height: 45px;
          cursor: pointer;
        }
      }
    }
    h2 {
      margin-bottom: 16px;
    }
  }
  .form-card {
    .demo-ruleForm {
      margin: 0 auto;
    }
  }
  .foot-card {
    .btn-group {
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
  .el-pagination {
    text-align: center;
    display: block;
    margin: 10px auto;
  }
}
</style>
